package bhbt.utils;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.MapUtils;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

public class MapUtil extends MapUtils {
	/**
	 * 将Map转换为Object
	 * 
	 * @param clazz
	 *            目标对象的类
	 * @param map
	 *            待转换Map
	 * @param toCamelCase
	 *            是否去掉下划线
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static <T, V> T toObject(Class<T> clazz, Map<String, V> map, boolean toCamelCase) 
		throws InstantiationException, IllegalAccessException, InvocationTargetException {
		T object = clazz.newInstance();
		return toObject(object, map, toCamelCase);
	}

	/**
	 * 将Map转换为Object
	 * @param object 目标对象
	 * @param map 待转换Map
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 */
	public static <T, V> T toObject(T object, Map<String, V> map)
			throws InstantiationException, IllegalAccessException,
			InvocationTargetException {
		return toObject(object, map, false);
	}

	public static <T, V> T toObject(T object, Map<String, V> map,
			boolean toCamelCase) throws InstantiationException,
			IllegalAccessException, InvocationTargetException {
		if (toCamelCase)
			map = toCamelCaseMap(map);
		BeanUtils.populate(object, map);
		return object;
	}

	/**
	 * 对象转Map
	 * @param object 目标对象
	 * @return
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * @throws NoSuchMethodException
	 */
	@SuppressWarnings("unchecked")
	public static Map<String, String> toMap(Object object)
			throws IllegalAccessException, InvocationTargetException,
			NoSuchMethodException {
		return BeanUtils.describe(object);
	}
	
	/** 
     * 将Map的Keys去下划线<br> 
     * (例:branch_no -> branchNo )<br> 
     * @param map 待转换Map 
     * @return 
     */  
    public static <V> Map<String, V> toCamelCaseMap(Map<String, V> map) {  
        Map<String, V> newMap = new HashMap<String, V>();  
        for (String key : map.keySet()) {
        	String field = StringUtil.toCamelCase(key);
            safeAddToMap(newMap, field, map.get(key));  
        }  
        return newMap;  
    }
    
    /**
     * @param map
     * @param key
     * @return
     */
    public static String[] getArray(final Map map, final Object key) {
        if (map != null) {
            Object answer = map.get(key);
            if (answer != null) {
            	if(answer instanceof Object[]){
            		return (String[])answer;
            	}
            	else{
            		String[] retArr = {answer.toString()};
            		return retArr;
            	}
            }
        }
        return new String[]{};
    }

	public static long getLongIgnoreCaseKey(final Map<String,Object> map, String getKey){
		if(map == null)return 0;
		for(Map.Entry<String, Object> ent : map.entrySet()){
			Object val = ent.getValue();
			if(ent.getKey().equalsIgnoreCase(getKey)){
				return null!=val?Long.valueOf(val.toString()):0;
			}
		}
		return 0;
	}
	public static String getStringIgnoreCaseKey(final Map<String,Object> map, String getKey){
		if(map == null)return "";
		for(Map.Entry<String, Object> ent : map.entrySet()){
			Object val = ent.getValue();
			if(ent.getKey().equalsIgnoreCase(getKey)){
				return null!=val?val.toString():"";
			}
		}
		return "";
	}
	
}
